package com.cws4cloud.cws.sdAdmin.biz.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.common.security.util.SecurityUtils;
import com.cws4cloud.cws.sdAdmin.api.entity.Param;
import com.cws4cloud.cws.sdAdmin.api.service.ParamService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 *
 *
 * @author zhulj
 * @date 2021-05-06 15:10:12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/param" )
@Api(value = "param", tags = "参数管理")
public class ParamController {

    private final  ParamService paramService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param param
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('sdAdmin_param_get')" )
    public R getParamPage(Page page, Param param) {
        return R.ok(paramService.page(page, Wrappers.query(param)));
    }

	@ApiOperation(value = "查询参数", notes = "查询参数")
	@GetMapping("/tree")
	@PreAuthorize("@pms.hasPermission('sdAdmin_param_get')" )
    public R getParamTree(boolean lazy, Integer parentId, String name){
    	return R.ok(paramService.listParamTrees(lazy,parentId,name));
	}

	@ApiOperation(value = "根据父级查询参数", notes = "根据父级查询参数")
	@GetMapping("/getParamByParent")
	public R getParamByParent(Integer parentId, String value){
		return R.ok(paramService.listByParent(parentId,value));
	}

    /**
     * 通过id查询
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('sdAdmin_param_get')")
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(paramService.getById(id));
    }

    /**
     * 新增
     * @param param
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('sdAdmin_param_add')")
    public R save(@RequestBody Param param) {
//    	param.setAdminId(SecurityUtils.getUserId());
        return R.ok(paramService.saveParam(param));
    }

    /**
     * 修改
     * @param param
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('sdAdmin_param_edit')" )
    public R updateById(@RequestBody Param param) {
        return R.ok(paramService.updateById(param));
    }

    /**
     * 通过id删除
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('sdAdmin_param_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(paramService.removeById(id));
    }

}
